﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using OnlineVideos.Data;
using Common.Library;
using System.ComponentModel;
using Microsoft.Phone.Controls;
using OnlineVideos.Utilities;
using OnlineVideos.UI;
using Common.Utilities;
using OnlineVideos.Entities;

namespace OnlineVideos
{
    public partial class StoryFavorites : UserControl
    {
        List<ShowLinks> showFavVideos = null;
        private bool IsDataLoaded;
        public StoryFavorites()
        {
            InitializeComponent();
            IsDataLoaded = false;
            showFavVideos = new List<ShowLinks>();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsDataLoaded == false)
            {
                GetPageDataInBackground();
                IsDataLoaded = true;
            }
        }
        private void lbxFavoritesongs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxFavoritesongs.SelectedIndex == -1)
                    return;

                string Url = (lbxFavoritesongs.SelectedItem as ShowLinks).LinkUrl;
                YoutubeApi.Youtube.PlayYoutubeVideo(Url);
                
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxFavoritesongs_SelectionChanged Method In Favorites file.", ex);
            }
            lbxFavoritesongs.SelectedIndex = -1;
        }
        private void btnFavSong_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OnlineVideos.View_Models.ShowDetail detailModel = new OnlineVideos.View_Models.ShowDetail();
                detailModel.AddToFavorites(lbxFavoritesongs, sender as MenuItem, LinkType.Songs);
                PageHelper.NavigateTo(NavigationHelper.MainPanoramaPage);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in btnFavSong_Click Method In Favorites file.", ex);
            }
        }
        private void GetPageDataInBackground()
        {
            BackgroundHelper bwHelper = new BackgroundHelper();
            bwHelper.AddBackgroundTask(
                                         (object s, DoWorkEventArgs a) =>
                                         {
                                             a.Result = FavoritesManager.GetFavoriteLinks();
                                         },
                                               (object s, RunWorkerCompletedEventArgs a) =>
                                               {
                                                   showFavVideos = (List<ShowLinks>)a.Result;
                                                   if (showFavVideos.Count > 0)
                                                   {
                                                       lbxFavoritesongs.ItemsSource = showFavVideos;

                                                       tblkFavNoSongs.Visibility = Visibility.Collapsed;
                                                   }
                                                   else
                                                   {
                                                       tblkFavNoSongs.Text = "No " + AppResources.FavoriteSongsPivotTitle + " available";
                                                       tblkFavNoSongs.Visibility = Visibility.Visible;
                                                   }
                                               }
                                             );
            bwHelper.RunBackgroundWorkers();
        }
    }
}
